package ch11;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class _19_HashMapEx3 {
	static Map<String, Map<String, String>> phoneBook = new HashMap<String, Map<String, String>>();

	public static void main(String[] args) {
		addPhoneNo("friend", "john", "010-111-1111");
		addPhoneNo("friend", "terry", "010-222-2222");
		addPhoneNo("friend", "jenifer", "010-333-3333");
		addPhoneNo("company", "kara", "010-444-4444");
		addPhoneNo("company", "kitty", "010-555-5555");
		addPhoneNo("company", "jodie", "010-666-6666");
		addPhoneNo("company", "jerry", "010-777-7777");
		addPhoneNo("clean", "010-888-8888");

		printList();
	}

	static void addPhoneNo(String groupName, String name, String tel) {
		addGroup(groupName);
		Map<String, String> contact = (Map<String, String>) phoneBook.get(groupName);
		contact.put(tel, name);
	}

	static void addGroup(String groupName) {
		if (!phoneBook.containsKey(groupName))
			phoneBook.put(groupName, new HashMap<String, String>());
	}

	static void addPhoneNo(String name, String tel) {
		addPhoneNo("etc", name, tel);
	}

	static void printList() {
		Set<Map.Entry<String, Map<String, String>>> groupSet = phoneBook.entrySet();
		for(Map.Entry<String,Map<String,String>> group:groupSet){
			Set<Map.Entry<String, String>> contactSet = group.getValue().entrySet();
			System.out.println(" * " + group.getKey() + "[" + contactSet.size()	+ "]");
			for(Map.Entry<String,String> contact:contactSet){
				String telNo = contact.getKey();
				String name = contact.getValue();
				System.out.println(name + ": " + telNo);
			}
			System.out.println();
		}	
	}
}
/*
 * etc[1]
clean: 010-888-8888

 * company[4]
jodie: 010-666-6666
jerry: 010-777-7777
kara: 010-444-4444
kitty: 010-555-5555

 * friend[3]
jenifer: 010-333-3333
terry: 010-222-2222
john: 010-111-1111
*/
